<!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Sprite Button</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
    <script src="../../build/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas, {
            mouse: new pc.Mouse(document.body),
            touch: new pc.TouchDevice(document.body),
            elementInput: new pc.ElementInput(canvas)
        });
        app.start();

        // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
        app.setCanvasResolution(pc.RESOLUTION_AUTO);

        window.addEventListener("resize", function () {
            app.resizeCanvas(canvas.width, canvas.height);
        });

        // Create a camera
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0, 0, 0)
        });
        app.root.addChild(camera);

        // Create a 2D screen
        var screen = new pc.Entity();
        screen.addComponent("screen", {
            referenceResolution: new pc.Vec2(1280, 720),
            scaleBlend: 0.5,
            scaleMode: pc.SCALEMODE_BLEND,
            screenSpace: true
        });
        app.root.addChild(screen);

        // Create a simple button
        var button = new pc.Entity();
        button.addComponent("button", {
            active: true,
            imageEntity: button,
            transitionMode: pc.BUTTON_TRANSITION_MODE_SPRITE_CHANGE
        });
        button.addComponent("element", {
            anchor: [ 0.5, 0.5, 0.5, 0.5 ],
            height: 64,
            pivot: [ 0.5, 0.5 ],
            type: pc.ELEMENTTYPE_IMAGE,
            width: 175,
            useInput: true
        });
        screen.addChild(button);

        // Create a label for the button
        var label = new pc.Entity();
        label.addComponent("element", {
            anchor: [ 0.5, 0.5, 0.5, 0.5 ],
            color: new pc.Color(1, 1, 1),
            fontSize: 32,
            height: 64,
            opacity: 0.5,
            pivot: [ 0.5, 0.5 ],
            text: "CLICK ME",
            type: pc.ELEMENTTYPE_TEXT,
            width: 128,
            wrapLines: true
        });
        button.addChild(label);

        // Change the background color every time the button is clicked
        button.button.on('click', function (e) {
            var r = Math.random();
            camera.camera.clearColor = new pc.Color(r, r, r);
        });

        // Move the button's label with the animation of the sprite
        button.button.on('pressedstart', function () {
            label.translateLocal(0, -4, 0);
        });
        button.button.on('pressedend', function () {
            label.translateLocal(0, 4, 0);
        });

        // Load a font
        var fontAsset = new pc.Asset('arial.json', "font", {url: "../assets/fonts/arial.json"});
        fontAsset.on('load', function () {
            // Apply the font to the text element
            label.element.fontAsset = fontAsset;
        });

        app.assets.add(fontAsset);
        app.assets.load(fontAsset);

        var textureAsset = new pc.Asset('red_button_atlas.png', "texture", {url: "../assets/button/red_button_atlas.png"});
        textureAsset.on('load', function () {
            var texture = textureAsset.resource;
            texture.addressU = pc.ADDRESS_CLAMP_TO_EDGE;
            texture.addressV = pc.ADDRESS_CLAMP_TO_EDGE;
            texture.minFilter = pc.FILTER_NEAREST;
            texture.magFilter = pc.FILTER_NEAREST;

            var atlas = new pc.TextureAtlas();
            atlas.frames = {
                "0": {
                    rect: new pc.Vec4(0, 147, 190, 49),
                    pivot: new pc.Vec2(0.5, 0.5),
                    border: new pc.Vec4(7, 11, 7, 7)
                },
                "1": {
                    rect: new pc.Vec4(0, 98, 190, 49),
                    pivot: new pc.Vec2(0.5, 0.5),
                    border: new pc.Vec4(7, 11, 7, 7)
                },
                "2": {
                    rect: new pc.Vec4(0, 49, 190, 49),
                    pivot: new pc.Vec2(0.5, 0.5),
                    border: new pc.Vec4(7, 11, 7, 7)
                },
                "3": {
                    rect: new pc.Vec4(0, 0, 190, 49),
                    pivot: new pc.Vec2(0.5, 0.5),
                    border: new pc.Vec4(7, 11, 7, 7)
                }
            };
            atlas.texture = texture;

            var createSpriteAsset = function (frame) {
                var sprite = new pc.Sprite(app.graphicsDevice, {
                    atlas: atlas,
                    frameKeys: frame,
                    pixelsPerUnit: 1,
                    renderMode: pc.SPRITE_RENDERMODE_SIMPLE
                });

                var spriteAsset = new pc.Asset('sprite', 'sprite', { url: '' });
                spriteAsset.resource = sprite;
                spriteAsset.loaded = true;
                app.assets.add(spriteAsset);
                return spriteAsset;
            }

            button.element.spriteAsset = createSpriteAsset('0');
            button.button.hoverSpriteAsset = createSpriteAsset('1');
            button.button.pressedSpriteAsset = createSpriteAsset('2');
            button.button.inactiveSpriteAsset = createSpriteAsset('3');
        });

        app.assets.add(textureAsset);
        app.assets.load(textureAsset);
    </script>
</body>
</html>
